package com.vfa.vocaburay;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.vfa.data.DateManipulation;
import com.vfa.data.toastMaking;

public class SearchOption extends Activity {
	private EditText dateOneET;
	private EditText dateTwoET;
	private EditText keyWordET;
	private Button searchByDate;
	private int mYear;
	private int mMonth;
	private int mDay;
	private String date1;
	private String date2;
	static final int DATE_DIALOG_ID_One = 0;
	static final int DATE_DIALOG_ID_Two = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchoption);
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		dateOneET = (EditText) findViewById(R.id.dateOne);
		dateTwoET = (EditText) findViewById(R.id.dateTwo);
		dateOneET.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
		dateTwoET.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
		// dateTwoET.setEnabled(false);
		keyWordET = (EditText) findViewById(R.id.keyWord);
		searchByDate = (Button) findViewById(R.id.searchButton);

		// on click
		searchByDate.setOnClickListener(search_listener);

		// on touch
		dateOneET.setOnTouchListener(dateOne_listener);
		dateTwoET.setOnTouchListener(dateTwo_listener);

	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(SearchOption.this, WordList.class);
		startActivity(intent);
		SearchOption.this.finish();
	}

	private OnClickListener search_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String a = dateOneET.getText().toString();
			String b = dateTwoET.getText().toString();
			String c = keyWordET.getText().toString();
			// all ""
			if (a.equals("") && b.equals("") && c.equals("")) {
				toastMaking.errorToast(getApplicationContext(),
						"Provide Search Information !").show();
			}
			// a "", b ko , c ""
			else if (a.equals("") && !b.equals("") && c.equals("")) {
				toastMaking.errorToast(getApplicationContext(),
						"Provide your first date !").show();
			}
			// a ko, b"", c""
			else if (!a.equals("") && b.equals("") && c.equals("")) {
				toastMaking.errorToast(getApplicationContext(),
						"Provide your second date !").show();
			}
			// a "', b "" , c k
			else if (a.equals("") && b.equals("") && !c.equals("")) {

				Intent intent = new Intent(SearchOption.this, WordList.class);
				intent.putExtra("searchCategory", "byKeyWord").putExtra(
						"keyWord", keyWordET.getText().toString());
				startActivity(intent);
				SearchOption.this.finish();

			} else if (!a.equals("") && !b.equals("") && c.equals("")) {
				if (DateManipulation.StringToDate(a) == null
						|| DateManipulation.StringToDate(b) == null) {
					toastMaking.errorToast(getApplicationContext(),
							"Check again your dates !").show();
				} else {
					Intent intent = new Intent(SearchOption.this,
							WordList.class);
					intent
							.putExtra("searchCategory", "byDate")
							.putExtra("dateOne", dateOneET.getText().toString())
							.putExtra("dateTwo", dateTwoET.getText().toString());
					startActivity(intent);
					SearchOption.this.finish();

				}
			} else if (!a.equals("") && !b.equals("") && !c.equals("")) {
				Intent intent = new Intent(SearchOption.this, WordList.class);
				intent.putExtra("searchCategory", "combo").putExtra(
						"dateOneCB", dateOneET.getText().toString()).putExtra(
						"dateTwoCB", dateTwoET.getText().toString()).putExtra(
						"keyWordCB", keyWordET.getText().toString());
				startActivity(intent);
				SearchOption.this.finish();
			}

		}
	};

	private OnTouchListener dateOne_listener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			showDialog(DATE_DIALOG_ID_One);
			return false;
		}
	};
	private OnTouchListener dateTwo_listener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			showDialog(DATE_DIALOG_ID_Two);
			return false;
		}
	};
	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener dateOne_Listener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			// set content for dateOneEditText
			String monthText = "";
			if (mMonth <= 9)
				monthText = "0" + mMonth;
			else 
				monthText = Integer.toString(mMonth);

			String dayText = "";
			if (dayOfMonth <= 9 )
				dayText = "0" + dayOfMonth;
			else
				dayText = Integer.toString(dayOfMonth);

			date1 = mYear + "-" + monthText + "-" + dayText;

			dateOneET.setText(mYear + "-" + monthText + "-" + dayText);

		}
	};
	private DatePickerDialog.OnDateSetListener dateTwo_Listener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			if (DateManipulation.StringToDate(dateOneET.getText().toString()) == null) {
				toastMaking.errorToast(getApplicationContext(),
						" Check your first date").show();
			} else {

			}
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			// set content for dateOneEditText
			String monthText = "";
			if (mMonth <= 9)
				monthText = "0" + mMonth;
			else
				monthText = Integer.toString(mMonth);

			String dayText = "";
			if (dayOfMonth <= 9)
				dayText = "0" + dayOfMonth;
			else
				dayText = Integer.toString(dayOfMonth);

			date2 = mYear + "-" + monthText + "-" + dayText;
			if (DateManipulation.StringToDate(date1).after(
					DateManipulation.StringToDate(date2))
					|| DateManipulation.StringToDate(date1).equals(
							DateManipulation.StringToDate(date2))) {
				toastMaking.errorToast(getApplicationContext(),
						"Provide suitable dates").show();
			} else {
				dateTwoET.setText(mYear + "-" + monthText + "-" + dayText);
			}
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID_One:
			return new DatePickerDialog(this, dateOne_Listener, mYear, mMonth,
					mDay);
		case DATE_DIALOG_ID_Two:
			return new DatePickerDialog(this, dateTwo_Listener, mYear, mMonth,
					mDay);
		}
		return null;
	}

}
